package com.leyou.user.web;

import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.UserContext;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/info")
public class UserController {

    @Autowired
    private UserService userService;
    /** todo:
     * data 要效验的数据
     * type 手机号或者密码
     * @return
     */
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> checkData(@PathVariable("data") String data,@PathVariable("type") Integer type){
        return ResponseEntity.ok(userService.checkData(data,type));
    }

    /** todo
     * 发送验证码
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone){
        userService.sendCode(phone);
        return ResponseEntity.ok().build();
    }

    /** todo
     * 用户注册
     * @param user
     * @param code
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> register(@Valid User user, @RequestParam("code") String code){
        userService.register(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /** todo
     * 根据用户名和密码查找用户
     * @param username
     * @param password
     * @return
     */
    @GetMapping
    public ResponseEntity<UserDTO> queryUserByUserNameAndPassword(@RequestParam("username")String username, @RequestParam("password")String password){
    return ResponseEntity.ok(userService.queryUserByUserNameAndPassword(username,password));
    }
    /**
     * 显示登陆的用户
     */
    @GetMapping("/me")
    public ResponseEntity<UserDetail> ShowUserName(){
        return ResponseEntity.ok(UserContext.getUser());
    }
 }
